import { fetchRandomDomain } from '@/common/domain.js';
const md5 = require('common/md5.min.js');
const getnow = Date.now || function() {
	return new Date().getTime();
};

let baseHost;

const storedDomain = uni.getStorageSync('domain');
if (storedDomain && storedDomain !== '') {
  baseHost = () => storedDomain + '/api/v2/';
} else {
  baseHost = () => 'https://5ltxkfoi.honghuan.asia:1433/api/v2/';
  //console.log('这里的数据',baseHost);
}
// 异步获取随机域名并更新 baseHost
async function fetchRandomDomainAndSetConfig() {
  try {
    const HostOK = await fetchRandomDomain();
	
    if (HostOK) {
      console.log('域名拼接成功:', HostOK);
	  baseHost = () => HostOK; // 更新 baseHost 的值为获取的随机域名

    } else {
      console.log('域名不可用');
    }
  } catch (error) {
    console.error('获取随机域名时发生错误:', error);
  }
}

// 调用异步函数以获取随机域名
fetchRandomDomainAndSetConfig();

export default {
	websiteUrl: baseHost(),
	//websiteUrl: 'https://www.honghuanjituan.com/api/v2/',
	//websiteUrl: 'http://lc.wf1.fun/api/v2/',
	time:60,
	now: function() {
		let time = getnow();
		return Math.ceil(time / 1000)
	},
	setSign: function(options = []) {
		options.sort();
		let str = options.join("&");
		return md5(str);
	},
	checkLogin: function() {
		var info = uni.getStorageSync("userinfo");
		if (info.username == '' || typeof(info.username) == 'undefined' || info.token == '' || typeof(info.token) ==
			'undefined') {
			uni.reLaunch({
				url: "../login/login"
			})
		}
	},
	//网络请求
	req:function(options){
		//对参数进行处理
		// console.log(this);
		//console.log('当前访问的是',this.websiteUrl)
		let api = options.api ==undefined ?"":options.api;
		let timestamp = options.timestamp ==undefined ?"":options.timestamp;
		let data = options.data ==undefined ?{}:options.data;
		let header = options.header ==undefined ?{}:options.header;
		let method = options.method ==undefined ?"POST":options.method;
		return new Promise((reslove,reject)=>{
			uni.request({
				url: this.websiteUrl + api +"/?timestamp=" + timestamp,
				data: data,
				header: header,
				method: method,
				success: (res) => {
//					console.log(res.data)
					
					// if (res.data.errorcode == undefined) {
					// 	uni.showToast({
					// 		title: "网络不佳",
					// 		icon: "none",
					// 		position: "bottom"
					// 	})
					// 	return;
					// }
					if (res.data.errorcode  == 19997){
						//退出当前登录
						uni.removeStorage('userinfo');	
						var pages = getCurrentPages();
						setTimeout(()=>{
							var currPage = pages.pop(); //当前页面
							console.log(currPage.$vm)
							if(pages.length == 0 || pages == undefined || pages == null){
								console.log('进入首页未登录跳转到login页')
								uni.navigateTo({
									url: "../login/login"
								})
							}else{
								currPage.onLoad();
							}
						},300)
					}
					//超时或者账号冻结
					if (res.data.errorcode  == 200000) {
						uni.showToast({
							title: res.data.message,
							icon: "none",
							// position: "bottom"
						})
						//退出当前登录
						uni.clearStorageSync('userinfo');
						setTimeout(()=>{
							uni.reLaunch({
								url: "../login/login"
							})
						},1000)
						return;
					}
					//需要实名认证
					if (res.data.errorcode  == 30000) {
						uni.showToast({
							title: res.data.message,
							icon: "none",
							// position: "bottom"
						})
						setTimeout(()=>{
							uni.navigateTo({
								url: '/pages/user/certification',
							});	
						},1000)
						return;
					}
					// console.log(res.data.errorcode);
					//发送成功
					if (res.data.errorcode  == 0) {
						reslove(res);
					}else{
						if(api == 'doLottery'){
							reslove(res);
						}else{
							if(api == 'getUserInfo'){
								uni.clearStorageSync('userinfo');
							}
							if(res.data.message){
								uni.showToast({
									title: res.data.message,
									icon: "none",
									// position: "bottom",
									duration: 2000
								})	
							}else{
								uni.showToast({
									title: '操作异常',
									icon: "none",
									// position: "bottom",
									duration: 2000
								})	
							}
							return;
						}
					}
					
				},
				fail: function() {
					uni.showToast({
						title: "网络异常",
						icon: "none",
						position: "bottom"
					})
				}
			})
		});
	}
}
